﻿<!---
 
  Copyright (c) 2010, Cristian Costantini - cristian@millemultimedia.it
  
  FileManager is licensed under the terms of the Open Source GPL 3.0 license. 
	
	http://www.gnu.org/licenses/gpl.html
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--->
<cfcomponent output="false" extends="fileManager.util.AbsService" >

	<cfset variables.STATIC.FILE_FORMAT = "jpg,png" />
	<cfset variables.STATIC.DEFAULT_WIDTH = 600 />
	<cfset variables.STATIC.DEFAULT_HEIGHT = 400 />

	<cffunction name="resize" access="public" returntype="Struct" >
		<cfargument name="source" type="string" required="true" hint="Absolute file path."/>
		<cfargument name="width" type="numeric" required="false" default="#variables.STATIC.DEFAULT_WIDTH#" hint="Width" />
		<cfargument name="height" type="numeric" required="false" default="#variables.STATIC.DEFAULT_HEIGHT#" hint="Height" />
		<cfset var result = structNew() />
		<cfset var image = "" />
		
		<cfset arguments.source = getAbsolutePath( arguments.source )/>
		
		<cftry>
			
			<!--- Check user permission --->
			<cfset validatePermission( "imageResize" ) />
		
			<cfif not listFindNoCase( variables.STATIC.FILE_FORMAT, listLast( arguments.source, '.' ) )>
				<cfthrow message="File type not supported." detail="ImageResize plug-in do not accept #listLast( arguments.source, '.' )# files." />
			</cfif>
			
			<cflock name="#arguments.source#" type="readonly" timeout="60" throwontimeout="true" >
				
				<cfset image = imageRead( arguments.source ) />
				
				<cfset imageResize( image, arguments.width, arguments.height ) />
		
				<cfset imageWrite( image, addSuffix( arguments.source, "_#arguments.width#_#arguments.height#" ) ) />
			
			</cflock>
			
			<cfset result['success'] = true />
									 
			<cfcatch type="any">
				
				<cfset result['success'] = false />
                <cfset result['type'] = "it.millemultimedia.fileManager.plugins.ImageResize" />
                <cfset result['detail'] = cfcatch.detail />
                <cfset result['message'] = cfcatch.message />
            	
				<cfset variables.eventManager.notifyOnError( error = result ) />
				
			</cfcatch>
		
		</cftry>
		
		<cfreturn result/>
	</cffunction>

<!--- 
/** 
* Copyright 2008 massimocorner.com
* addSuffix
* @author      Massimo Foti (massimo@massimocorner.com).
* @version     0.6.1, 2008-12-12
*/
 --->
 
	<cffunction name="addSuffix" access="private" output="true" returntype="string" hint="Utility method. Adds a prefix to a filename">
		<cfargument name="path" type="string" required="yes" hint="File path, either local or absolute">
		<cfargument name="suffixString" type="string" required="no" default="_thumb" hint="String used as prefix">
		<cfscript>
		var prefixedPath = "";
		var dirPath = GetDirectoryFromPath(arguments.path);
		var fileExtension = ListLast(arguments.path, ".");
		var filename = ListFirst(GetFileFromPath(arguments.path), ".");
		// If the directory is the current one, skip it
		if((dirPath EQ "/") OR (dirPath EQ "\")){
			dirPath = "";
		}
		return dirPath & filename & arguments.suffixString & "." & fileExtension;
		</cfscript>
	</cffunction>

</cfcomponent>